﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dao;
using common;
using System.Resources;
using System.Globalization;
namespace GTVAccountingSystem.view.usercontrol
{
    public partial class uclAuditlog : UserControl
    {
        AuditLogDAO auditLogDAO = new AuditLogDAO();
        public String username = "";
        public String fromDate = "";
        public String endDate = "";
        public DataTable dt;
        ResourceManager rm = Constant.RM;
        CultureInfo culture = Constant.CULTURE;
        public uclAuditlog()
        {
            InitializeComponent();
            DataTable dt = auditLogDAO.getAllActionsDetailsByEnglishID("1");
            cboActions.DataSource = dt;
            cboActions.DisplayMember = "Contents";
            cboActions.ValueMember = "ID";
            loadAuditLogData();
            setLanguage();
        }
        /* Author : Jenny
         * Date : 10/4/2012
         * function setLanguage
         * set language
         * no return
         */
        public void setLanguage()
        {
            lblUsername.Text = rm.GetString("lblUsername", culture) + " : ";
            lblDateBegin.Text = rm.GetString("lblDateBegin", culture) + " : ";
            lblDateEnd.Text = rm.GetString("lblDateEnd", culture) + " : ";
            btnSearch.Text = Constant.TAB + rm.GetString("btnSearch", culture);
            btnPreview.Text = Constant.TAB + rm.GetString("btnPreview", culture);
            this.Text = rm.GetString("frmAuditLog", culture);
            dgvAuditLog.DataSource = dt;
            dgvAuditLog.Columns["UserName"].Name = rm.GetString("lblUserName", culture);
            dgvAuditLog.Columns["StaffCode"].Name = rm.GetString("lblStaffCode", culture);
            dgvAuditLog.Columns["FirstName"].Name = rm.GetString("lblFullName", culture);
            dgvAuditLog.Columns["LogDate"].Name = rm.GetString("lblLogDate", culture);
            dgvAuditLog.Columns["LogTime"].Name = rm.GetString("lblLogTime", culture);
            dgvAuditLog.Columns["Actions"].Name = rm.GetString("lblActions", culture);
            dgvAuditLog.Columns["Contents"].Name = rm.GetString("lblContents", culture);
            
        }
        /* Author : Jenny
         * Date : 3/4/2012
         * function loadAuditLogData
         * get auditlog data and load to datagridview
         * no return 
         */
        public void loadAuditLogData()
        {
            username = txtUserName.Text.Trim();
            fromDate = auditLogDAO.sortDate(dtpFromDate.Value.ToShortDateString());
            endDate = auditLogDAO.sortDate(dtpEndDate.Value.ToShortDateString());
            String status = "1";
            if (chkDate.Checked == true)
                status = "2";
            dt = auditLogDAO.getAuditLogByCondition(username, fromDate, endDate, cboActions.SelectedValue.ToString(), status);
            dgvAuditLog.DataSource = dt;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            loadAuditLogData();
        }

        private void chkDate_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDate.Checked == true)
            {
                dtpFromDate.Enabled = true;
                dtpEndDate.Enabled = true;
            }
            else
            {
                dtpFromDate.Enabled = false;
                dtpEndDate.Enabled = false;
            }
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            frmAuditLogPreview auditLogPreview = new frmAuditLogPreview(this);
            auditLogPreview.ShowDialog();
            
        }

        private void dgvAuditLog_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            dt.DefaultView.Sort = dt.Columns[e.ColumnIndex].ColumnName + " ASC";
        }
    }
}
